<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物资编号" prop="assetsCode">
						<el-input v-model="form.assetsCode" placeholder="资产编号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="资产名称" prop="assetsName">
						<el-input v-model="form.assetsName" placeholder="资产名称" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="所属分类" prop="sortId">
						<el-cascader v-model="form.sortId" :options="fixedSort" :props="fixedSortProps" :show-all-levels="false" clearable style="width: 100%"></el-cascader>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="使用部门" prop="ownDept">
						<select-dept-dialog v-model:deptId="form.ownDept" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="存放仓库" prop="storageId">
						<sc-select v-model="form.storageId" :apiObj="$API.office.fixed.getFixedAssetsStorageList" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="品牌" prop="brand">
						<el-input v-model="form.brand" placeholder="品牌" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="型号" prop="model">
						<el-input v-model="form.model" placeholder="型号" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="采购价格" prop="purchasePrice">
						<el-input-number v-model="form.purchasePrice" placeholder="采购价格" clearable style="width: 100%;"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="采购时间" prop="purchaseTime">
						<el-date-picker v-model="form.purchaseTime" placeholder="采购时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="折旧方式" prop="depreciation">
						<el-select v-model="form.depreciation" style="width: 100%">
							<el-option value="0" label="平均年限法"></el-option>
							<el-option value="1" label="工作量折旧法"></el-option>
							<el-option value="2" label="双倍余额递减法"></el-option>
							<el-option value="3" label="年数总合法"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="计量单位" prop="unit">
						<sc-select v-model="form.unit" :params="{code:'unit'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'fixedattach'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectDeptDialog from "@/views/module/selectDeptDialog";
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		selectDeptDialog,
		AttachMenu
	},
	data() {
		return {
			apiObj:this.$API.file.attach.uploadFile,
			fileList:[],
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '资产录入对话框',
				edit: '编辑资产对话框',
			},
			fixedSort: [],
			fixedSortProps: {
				value: "sortId",
				label: "sortName",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				assetsId: '',
				sortNo: 0,
				assetsName: '',
				assetsCode: '',
				sortId: '',
				brand: '',
				model: '',
				depreciation: '1',
				purchasePrice: 0,
				purchaseTime: '',
				storageId:'',
				unit: '',
				attachId:'',
				ownDept: '',
				remark: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				assetsName: [
					{required: true, message: '资产名称不能为空', trigger: 'blur'}
				],
				sortId: [
					{required: true, message: '资产分类不能为空', trigger: 'blur'}
				],
				assetsCode: [
					{required: true, message: '资产编号不能为空', trigger: 'blur'}
				],
				unit: [
					{required: true, message: '请选择计量单位', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getFixedAssetsSortGroup();
	},
	methods: {
		async getFixedAssetsSortGroup() {
			var res = await this.$API.office.fixed.getFixedAssetsSortTree.get();
			this.fixedSort = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.fixed.insertFixedAssets.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.fixed.updateFixedAssets.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.assetsId = data.assetsId
			this.form.sortNo = data.sortNo
			this.form.assetsName = data.assetsName
			this.form.assetsCode = data.assetsCode
			this.form.sortId = data.sortId
			this.form.brand = data.brand
			this.form.model = data.model
			this.form.storageId = data.storageId
			this.form.attachId = data.attachId
			this.form.depreciation = data.depreciation
			this.form.purchasePrice = data.purchasePrice
			this.form.purchaseTime = data.purchaseTime
			this.form.unit = data.unit
			this.form.ownDept = data.ownDept
			this.form.remark = data.remark

		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
